/**
 * refer to http://developer.mixi.co.jp/appli/spec/pc/get_mymixi_info#toc-1
 */
function getViewerInfo(){
    var req = opensocial.newDataRequest();
    req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER), "viewer");
    req.send(function(data){
        if (!data.hadError()) {
            var item = data.get("viewer");
            if (item.hadError()) {
                var code = item.getErrorCode();
                // error logic
            }
            else {
                var viewer = item.getData();
                var id = viewer.getId();
                var nickname = viewer.getDisplayName();
                var thumbnailUrl = viewer.getField(opensocial.Person.Field.THUMBNAIL_URL);
                // 
            }
        }
    });
}

/**
 * refer to http://developer.mixi.co.jp/appli/spec/pc/send_activity
 * @param {Object} fieldTitle
 * @param {Object} receipents
 */
function sendActivity(fieldTitle, receipents){
    var appId = APP_ID
    var data = {};
    var params = {};
    
    params[opensocial.Activity.Field.TITLE] = "[運動会]" + fieldTitle + "1位:" + playersList[receipents[0]][NICKNAME] + "さん 2位:" + playersList[receipents[1]][NICKNAME] + "さん";
    params[opensocial.Activity.Field.URL] = "http://mixi.jp/view_appli.pl?id=" + appId;
    params[mixi.ActivityField.RECIPIENTS] = receipents;
    var activity = opensocial.newActivity(params);
    opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH, function(res){
        if (res.hadError()) {
            return true;
        }
        return false;
    });
    
}

function changePictureSize(url, size){
    if (size == 40) { //40px
        return url.replace("s.jpg", ".jpg");
        
    }
    else // 180px
         if (size == 180) {
            return url.replace("s.jpg", "m.jpg");
        }
    // 72px
    return url;
}

/**
 *  refer to http://developer.mixi.co.jp/appli/spec/pc/send_message
 */
function sendMessage(){
    var title = "hoge";
    var body = "age";
    var recipient = "3891821";
    var params = {};
    params[opensocial.Message.Field.TITLE] = title;
    var msg = opensocial.newMessage(body, params);
    opensocial.requestSendMessage(recipient, msg, function(response){
        if (response.hadError()) {
            var code = response.getErrorCode();
            
        }
    });
    
    
}

/**
 *  refer to http://developer.mixi.co.jp/appli/spec/pc/invite_appli
 */
function inviteFriends(){
    var params = {};
    ///    params[opensocial.IdSpec.Field.USER_ID] = inviteFriendIDs;
    ///var recipients = opensocial.newIdSpec(params);
    opensocial.requestShareApp("VIEWER_FRIENDS", null, function(response){
        if (response.hadError()) {
            var errCode = response.getErrorCode();
        }
        else {
            var recipientIds = response.getData()["recipientIds"];
        }
    });
}

/**
 * refer to http://developer.mixi.co.jp/connect/mixi_plugin/simplepost/spec_of_simplepost
 * @param {Object} index
 */
function writeDiary(index){
    title = "運動会の結果";
    var url = "占い!マイミク学園-運動会編- \r\nhttp://mixi.jp/view_appli.pl?id=" + APP_ID;
    titleEncode = encodeURI(title);
    var diary = diaryText[index];
    diaryTextEncode = encodeURI(url + diaryText[index]);
    location.href = "http://mixi.jp/simplepost/diary?title=" + titleEncode + "&body=" + diaryTextEncode;
}

/**
 * refer to http://developer.mixi.co.jp/appli/spec/pc/send_voice
 * @param {Object} index
 */
function doVoice(index){
    var params = {};
    // When you want to add URL in voice, please 
    params[mixi.Status.Field.URL] = "http://mixi.jp/view_appli.pl?id=" + APP_ID + "&appParams=";
    params[mixi.Status.Field.MOBILE_URL] = "http://ma.mixi.net/" + APP_ID + "/?url=";
    params[mixi.Status.Field.TOUCH_URL] = "http://mixi.jp/view_appli.pl?id=" + APP_ID + "&appParams=";
    var voice = voiceText[index];
    mixi.requestUpdateStatus(voiceText[index], function(response){
        if (response.hadError()) {
            var code = response.getErrorCode();
            var msg = response.getErrorMessage();
            
        }
        else {
        
        }
    }, params);
}


